package com.example.it.studynotification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RemoteViews;

public class MainActivity extends AppCompatActivity {

    private int REQUEST_CODE = 1;
    private final int UPDATE_ID = 1;
    private Notification.Builder nBuilder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View view) {

        //保留Activity的栈的方式设置导航
        //创建一个notification
        nBuilder = new Notification.Builder(this).
                setSmallIcon(R.drawable.notification).
                setContentTitle("The is a notification!").
                setContentText("The is notification!The is notification!The is notification!The is notification!");
        //获取通知管理者
        NotificationManager nManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(this, SecondActivity.class);
        //实例化堆栈创建者
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        //添加返回栈
        stackBuilder.addParentStack(SecondActivity.class);
        //将我们的意图添加到返回栈的顶部
        stackBuilder.addNextIntent(intent);
        //获取PendingIntent的对象
        PendingIntent pi = stackBuilder.getPendingIntent(this.REQUEST_CODE, PendingIntent.FLAG_UPDATE_CURRENT);

        //设置进图条通知栏
        nBuilder.setProgress(100, 50, true);

        nBuilder.setContentIntent(pi);

        nManager.notify(this.UPDATE_ID, nBuilder.build());

        /*普通的方式设置通知的导航
        Notification.Builder nBuilder = new Notification.Builder(this)
                .setContentText("Content of notification")
                .setContentTitle("Title of notification")
                .setSmallIcon(R.drawable.notification);
        Intent intent = new Intent(this, SecondActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivities(this, this.REQUEST_CODE, new Intent[]{intent}, PendingIntent.FLAG_UPDATE_CURRENT);

        nBuilder.setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, nBuilder.build());*/
    }

    //更新通知
    public void clickUpdate(View view) {
        nBuilder.setContentTitle("更新后的标题");
        NotificationManager nManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(this.UPDATE_ID, nBuilder.build());
    }

    //显示自定义通知栏
    public void clickCustom(View view) {
        /*
        * 远程视图
        * 从我们预先定义好的布局文件中，加载我们的自定义notification的布局
        * */
        RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.layout_custom_notification_money);

        //设置自定义布局的内容
        views.setTextViewText(R.id.tv_Title, "您的彩票中奖了500w");
        views.setTextViewText(R.id.tv_Content, "领奖暗号是:天王盖地虎!");
        //虽然我们的 btn_View 是按钮，但是他是继承是我们的TextView所以设置Button的text时，也是按照textview的方式来设置
        views.setTextViewText(R.id.btn_View, "暴富");

        //为按钮设置点击事件
        Intent intent = new Intent(this, MoneyActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivities(this, 1, new Intent[]{intent}, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.btn_View, pendingIntent);
        //将我们的自定义视图绑定到我们的自定的通知
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContent(views);

        //这行代码一定要保留，我们要用它来显示我们通知的icon,如果不设置就会报错的。
        builder.setSmallIcon(R.drawable.notification);


        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(1, builder.build());
    }

    public void clickNotification(View view) {

    }
}
